#include "main_window.h"

gint delete_event(GtkWidget *window){
	gtk_main_quit();
	return FALSE;
}

int main_window(char *userinfo){
	GtkWidget *window;
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS);
	gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
	gtk_window_set_title(GTK_WINDOW(window), "calou client");
	g_signal_connect(G_OBJECT(window), "delete_event",G_CALLBACK(delete_event), NULL);
	gtk_container_set_border_width(GTK_CONTAINER(window), 10);
	
	display_window(window, userinfo);
	
	gtk_widget_set_size_request(GTK_WIDGET(window), 200, 450);
	gtk_widget_show_all(window);
	return 1;
}

void display_window(GtkWidget *window, char *userinfo){
	GtkWidget *layout;

	GtkWidget *info;
	GtkWidget *list;
	GtkWidget *toolbar;
	
	layout = gtk_vbox_new(FALSE, 0);
	gtk_container_add(GTK_CONTAINER(window), layout);

	char *userName = socket_getUserName(userinfo);
	char *userFlag = socket_setUserFlag(userinfo);
	char *userFriendsId = socket_getUserFriends(userinfo);

	info = create_userinfo(userName);
	gtk_box_pack_start(GTK_BOX(layout), info, FALSE, FALSE, 3);
	
	list = create_list(userFriendsId);
	gtk_box_pack_start(GTK_BOX(layout), list, TRUE, TRUE, 3);
	
	toolbar = create_toolbar();
	gtk_box_pack_start(GTK_BOX(layout), toolbar, FALSE, FALSE, 3);
}

GtkWidget *create_userinfo(char *userName){
	GtkWidget *info;
	GtkWidget *imageButton;
	GtkWidget *image;
	GtkWidget *nameLabel;
	GtkWidget *flagLabel;
	GtkWidget *colorbtn;
	GtkWidget *fixed;
	
	info = gtk_hbox_new(FALSE, 0);
	gtk_container_set_border_width(GTK_CONTAINER(info), 2);
	gtk_widget_set_size_request(GTK_WIDGET(info), 250, 60);

	GdkPixbuf *pixbuf = NULL;
	pixbuf = gdk_pixbuf_new_from_file("./image/User.png", NULL);
	pixbuf = gdk_pixbuf_scale_simple(pixbuf, 52, 50, GDK_INTERP_BILINEAR);
	image = gtk_image_new_from_pixbuf(pixbuf);
	imageButton = gtk_button_new();
	gtk_button_set_image(GTK_BUTTON(imageButton), image);
	gtk_box_pack_start(GTK_BOX(info), imageButton, FALSE, FALSE, 3);

	fixed = gtk_fixed_new();
	gtk_box_pack_start(GTK_BOX(info), fixed, FALSE, FALSE, 0);

	nameLabel = gtk_label_new(userName);
	flagLabel = gtk_label_new("online");
	gtk_fixed_put(GTK_FIXED(fixed), nameLabel, 0, 0);
	gtk_widget_set_size_request(nameLabel, 100, 20);
	gtk_fixed_put(GTK_FIXED(fixed), flagLabel, 10, 20);
	gtk_widget_set_size_request(flagLabel, 100, 20);

	image = gtk_image_new_from_stock(GTK_STOCK_SELECT_COLOR, GTK_ICON_SIZE_SMALL_TOOLBAR);
	colorbtn = gtk_button_new();
	gtk_button_set_image(GTK_BUTTON(colorbtn), image);
	gtk_fixed_put(GTK_FIXED(fixed), colorbtn, 80, 40);
	gtk_widget_set_size_request(colorbtn, 26, 26);

	return info;
}

GtkWidget *create_list(char *userFriendsId){
	GtkWidget *scrolled_window;
	GtkWidget *tree_view;
	GtkListStore *list;
	GtkCellRenderer *renderer;
	GtkTreeViewColumn *column;
	
	scrolled_window = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

	list = show_list();
	
	tree_view = gtk_tree_view_new();
	gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_window), tree_view);
	gtk_tree_view_set_model(GTK_TREE_VIEW(tree_view), GTK_TREE_MODEL(list));
	
	renderer = gtk_cell_renderer_text_new();
	column = gtk_tree_view_column_new_with_attributes("friends", renderer, "text", NAME, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), GTK_TREE_VIEW_COLUMN(column));

	GtkTreeSelection *select;

	select = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
	gtk_tree_selection_set_mode(select, GTK_SELECTION_SINGLE);

	g_signal_connect(tree_view, "row-activated", G_CALLBACK(show_talk_win_clicked), NULL);

	return scrolled_window;
}

GtkListStore *show_list(void){
	GtkTreeIter iter;
	GtkListStore *list;
	list = gtk_list_store_new(1, G_TYPE_STRING);
	int i = 0 ;

	for(i = 0 ; i < 10 ; i++){
		gchar *name = g_strdup_printf("a %d",i);
		
		gtk_list_store_append(GTK_LIST_STORE(list), &iter);
		gtk_list_store_set(GTK_LIST_STORE(list), &iter, NAME, name, -1);
		
		g_free(name);
	}
	return list;
}

void show_talk_win_clicked(GtkTreeView *tree_view, GtkTreePath *path, gpointer data){
	
	GtkTreeIter iter;
	GtkTreeModel *model;

	model = gtk_tree_view_get_model(tree_view);

	if(gtk_tree_model_get_iter(model, &iter,path)){
		gchar *name;
		gtk_tree_model_get(model, &iter, NAME, &name, -1);
		printf("%s\n",name);
		show_talk_win();
		g_free(name);
	}else{
		printf("nothing is selected!\n");
	}
}

GtkWidget *create_toolbar(void){
	GtkWidget *toolbar;
	GtkWidget *vtoolbar;
	GtkWidget *htoolbar;

	GtkWidget *SetCenter;
	GtkWidget *Set;
	GtkWidget *Color;
	GtkWidget *Find;
	GtkWidget *image;

	toolbar = gtk_hbox_new(FALSE, 0);
	vtoolbar = gtk_vbox_new(FALSE, 0);
	htoolbar = gtk_hbox_new(FALSE, 0);

	GdkPixbuf *pixbuf;
	GdkPixmap *pixmap;
	GdkBitmap *bitmap;
	GtkWidget *eventbox;
	
	eventbox = gtk_event_box_new();
	gtk_widget_set_events(eventbox, GDK_MOTION_NOTIFY|GDK_BUTTON_PRESS
			|GDK_BUTTON_RELEASE|GDK_ENTER_NOTIFY|GDK_LEAVE_NOTIFY);
	g_signal_connect(G_OBJECT(eventbox), "button_press_event", GTK_SIGNAL_FUNC(mouse_event_handle), NULL);
	g_signal_connect(G_OBJECT(eventbox), "button_release_event", GTK_SIGNAL_FUNC(mouse_event_handle), NULL);
	g_signal_connect(G_OBJECT(eventbox), "enter_notify_event", GTK_SIGNAL_FUNC(mouse_event_handle), NULL);
	g_signal_connect(G_OBJECT(eventbox), "leave_notify_event", GTK_SIGNAL_FUNC(mouse_event_handle), NULL);

	gtk_box_pack_start(GTK_BOX(toolbar), eventbox, FALSE, FALSE, 0);
	pixbuf = gdk_pixbuf_new_from_file("./image/lovely.ico", NULL);
	pixbuf = gdk_pixbuf_scale_simple(pixbuf, 48, 48, GDK_INTERP_BILINEAR);
	image = gtk_image_new_from_pixbuf(pixbuf);
	gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &bitmap, 128);
	gtk_widget_shape_combine_mask(eventbox, bitmap, 0, 0);
	gtk_container_add(GTK_CONTAINER(eventbox), image);

	gtk_box_pack_start(GTK_BOX(toolbar), vtoolbar, FALSE, FALSE, 3);
	image = gtk_image_new_from_stock(GTK_STOCK_EXECUTE, GTK_ICON_SIZE_SMALL_TOOLBAR);
	Set = gtk_button_new();
	gtk_button_set_image(GTK_BUTTON(Set), image);
/*
	image = gtk_image_new_from_stock(GTK_STOCK_SELECT_COLOR, GTK_ICON_SIZE_SMALL_TOOLBAR);
	Color = gtk_button_new();
	gtk_button_set_image(GTK_BUTTON(Color), image);
*/
	image = gtk_image_new_from_stock(GTK_STOCK_FIND, GTK_ICON_SIZE_SMALL_TOOLBAR);
	Find = make_box("查找", image);

	gtk_box_pack_start(GTK_BOX(toolbar), Set, FALSE, FALSE, 3);	
//	gtk_box_pack_start(GTK_BOX(vtoolbar), htoolbar, FALSE, FALSE, 3);
	gtk_box_pack_start(GTK_BOX(toolbar), Find, FALSE, FALSE, 3);
	
/*
	gtk_box_pack_start(GTK_BOX(htoolbar), Set, FALSE, FALSE, 3);
	gtk_box_pack_start(GTK_BOX(htoolbar), Color, FALSE, FALSE, 3);
*/
	return toolbar;
}

gint mouse_event_handle(GtkWidget *widget, GdkEventButton *event, gpointer data){

	switch(event->button){
		case 1:
			printf("Left clicked ");
			break;
		case 2:
			printf("Middle clicked ");
			break;
		case 3:
			printf("Right clicked ");
			break;
		default:
			printf("Unknown ");
	}
	GdkPixbuf *pixbuf = NULL;
	GdkPixmap *pixmap = NULL;
	GdkBitmap *bitmap = NULL;
	GtkWidget *oldImage = NULL;
	GtkWidget *newImage = NULL;
	switch(event->type){
		case GDK_BUTTON_PRESS:
		//	printf("Mouse button press at (%.2f, %.2f)\n", event->x, event->y);
			oldImage = GTK_WIDGET((gtk_container_get_children(GTK_CONTAINER(widget)))->data);
			gtk_object_ref(GTK_OBJECT(oldImage));
			gtk_container_remove(GTK_CONTAINER(widget), oldImage);
			pixbuf = gdk_pixbuf_new_from_file("./image/smile.ico", NULL);
			pixbuf = gdk_pixbuf_scale_simple(pixbuf, 48, 48, GDK_INTERP_BILINEAR);
			newImage = gtk_image_new_from_pixbuf(pixbuf);
			gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &bitmap, 128);
			gtk_widget_shape_combine_mask(widget, bitmap, 0, 0);
			gtk_container_add(GTK_CONTAINER(widget), newImage);
			gtk_widget_show(newImage);
			break;
		case GDK_BUTTON_RELEASE:
		//	printf("Mouse button release at (%.2f, %.2f)\n", event->x, event->y);
			oldImage = GTK_WIDGET((gtk_container_get_children(GTK_CONTAINER(widget)))->data);
			gtk_object_ref(GTK_OBJECT(oldImage));
			gtk_container_remove(GTK_CONTAINER(widget), oldImage);
			pixbuf = gdk_pixbuf_new_from_file("./image/smile.ico", NULL);
			pixbuf = gdk_pixbuf_scale_simple(pixbuf, 48, 48, GDK_INTERP_BILINEAR);
			newImage = gtk_image_new_from_pixbuf(pixbuf);
			gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &bitmap, 128);
			gtk_widget_shape_combine_mask(widget, bitmap, 0, 0);
			gtk_container_add(GTK_CONTAINER(widget), newImage);
			gtk_widget_show(newImage);
			break;
		case GDK_ENTER_NOTIFY:
		//	printf("Mouse enter.\n");
			oldImage = GTK_WIDGET((gtk_container_get_children(GTK_CONTAINER(widget)))->data);
			gtk_object_ref(GTK_OBJECT(oldImage));
			gtk_container_remove(GTK_CONTAINER(widget), oldImage);
			pixbuf = gdk_pixbuf_new_from_file("./image/smile.ico", NULL);
			pixbuf = gdk_pixbuf_scale_simple(pixbuf, 48, 48, GDK_INTERP_BILINEAR);
			newImage = gtk_image_new_from_pixbuf(pixbuf);
			gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &bitmap, 128);
			gtk_widget_shape_combine_mask(widget, bitmap, 0, 0);
			gtk_container_add(GTK_CONTAINER(widget), newImage);
			gtk_widget_show(newImage);
			break;
		case GDK_LEAVE_NOTIFY:
		//	printf("Mouse leave.\n");		
			oldImage = GTK_WIDGET((gtk_container_get_children(GTK_CONTAINER(widget)))->data);
			gtk_object_ref(GTK_OBJECT(oldImage));
			gtk_container_remove(GTK_CONTAINER(widget), oldImage);
			pixbuf = gdk_pixbuf_new_from_file("./image/lovely.ico", NULL);
			pixbuf = gdk_pixbuf_scale_simple(pixbuf, 48, 48, GDK_INTERP_BILINEAR);
			newImage = gtk_image_new_from_pixbuf (pixbuf);
			gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &bitmap, 128);
			gtk_widget_shape_combine_mask(widget, bitmap, 0, 0);
			gtk_container_add(GTK_CONTAINER(widget), newImage);
			gtk_widget_show(newImage);
			break;
		default:
			printf("\n");
			break;
	}
	return FALSE;
}

GtkWidget *make_box(gchar *title, GtkWidget *image){
	
	GtkWidget *layout;
	GtkWidget *button;
	GtkWidget *label;

	layout = gtk_hbox_new(FALSE, 0);
	
	label = gtk_label_new(title);
	button = gtk_button_new();
	
	gtk_box_pack_start(GTK_BOX(layout), image, FALSE, FALSE, 3);
	gtk_box_pack_start(GTK_BOX(layout), label, FALSE, FALSE, 3);
	
	gtk_container_add(GTK_CONTAINER(button), layout);
	
	return button;
	
}

